import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

class SelfHttp extends StatefulWidget {
  @override
  _SelfHttpState createState() => _SelfHttpState();
}

class _SelfHttpState extends State<SelfHttp> {
  List _list = [];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("http 请求数据")),
      body: ListView.builder(
        itemCount: _list.length,
        itemBuilder: (BuildContext context, int i) =>
            Text('${_list[i]["nm"]}'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          httpRequest1();
        },
        child: Icon(Icons.search),
      ),
    );
  }

  void httpRequest1() {
    http
        .get('http://m.maoyan.com/ajax/movieOnInfoList?token=')
        .then((http.Response response) {
      final Map<String, dynamic> responseData = jsonDecode(response.body);
      print('数据: ${responseData['movieList']}');
      setState(() {
        _list = responseData['movieList'];
      });
    }).catchError((error) {
      print('错误原因: $error');
    });
  }

  void httpRequest2() async {
    try {
      String url =
          'http://m.maoyan.com/ajax/moreComingList?token=&movieIds=1205290%2C1203775%2C894477%2C1176486%2C1242130%2C1206717%2C1281118%2C1217246%2C1262590%2C1246335';
      http.Response response = await http.get('$url');
      final Map<String, dynamic> responseData = json.decode(response.body);
      print('数据是${responseData["coming"]}');
      setState(() {
        _list = responseData["coming"];
      });
    } catch (error) {
      print('错误$error');
    }
  }

}
